/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.staf.SnmpTopologyExplorer.V2;

import java.util.ArrayList;
import java.util.Set;
import java.util.Iterator;
import java.io.File;
import java.io.IOException;
import jxl.*;
import jxl.write.*;

import ru.staf.SnmpTopologyExplorer.NetScanner;
import ru.staf.SnmpTopologyExplorer.Node;

/**
 *
 * @author aezhov
 */
public class TopologyModeler {

    ArrayList<Switch> members = new ArrayList<Switch>();
    ArrayList<Switch> unknownMembers = new ArrayList<Switch>();
    String reportFilename = null;
    int unknownNumber = 1;

    public void addSwitch(Switch member) {
        if (member == null) {
            return;
        }

        members.add(member);
    }

    public void setFilename(String reportFilename) {
        this.reportFilename = reportFilename;
    }

    public Switch registerAnonymousSwitch(Switch requestor, Set<String> macs) {
        String unknownName = "UNKN-" + (unknownNumber++);
        Switch sw = new Switch(requestor, unknownName, macs);
        unknownMembers.add(sw);
        
        return sw;
    }

    public void calculateCrossSwitchRelations() throws java.io.IOException,
        jxl.write.WriteException
    {
        int nSw = members.size();

        // Step 1. Initialize cross-switch potential links array in every switch
        for (int i = 0; i < nSw; i++) {
            Switch sw = members.get(i);
            sw.calculateCrossSwitchRelations(this, members);
        }

        // Step 2.
        members.addAll(unknownMembers);
        nSw = members.size();
        for (int i = 0; i < nSw; i++) {
            Switch sw = members.get(i);
            sw.calculateNearestNeighbours();
        }

        // Step 3. Check logical consistency of the topology
        for (int i = 0; i < nSw; i++) {
            Switch sw = members.get(i);
            sw.checkLinkConsistency();
        }

        // Step 4. Create final report in form of Excel book
        File excelFile = new File(reportFilename);
        WritableWorkbook wb = Workbook.createWorkbook(excelFile);
        WritableSheet currentSheet = null;
        int sheetIndex = 0;
        WritableFont bfont = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD);
        WritableCellFormat bold = new WritableCellFormat(bfont);
        Iterator<Switch> swIt = members.iterator();

        while (swIt.hasNext()) {
            int row = 1;
            Switch sw = swIt.next();
            NetScanner netScanner = sw.netScanner;

            currentSheet = wb.createSheet(sw.name, sheetIndex++);
            currentSheet.addCell(new Label(0, 0, "Транки", bold));
            currentSheet.addCell(new Label(1, 0, "Порт", bold));
            currentSheet.addCell(new Label(2, 0, "Сосед", bold));
            currentSheet.setColumnView(0, 22);
            currentSheet.setColumnView(1, 8);
            currentSheet.setColumnView(2, 20);
            currentSheet.setColumnView(3, 15);
            currentSheet.setColumnView(4, 20);
            currentSheet.setColumnView(5, 30);
            currentSheet.setColumnView(6, 160);
            Set<Integer> portNums = sw.linkTable.keySet();
            Iterator<Integer> portIt = portNums.iterator();

            while (portIt.hasNext()) {
                Integer port = portIt.next();
                currentSheet.addCell(new Label(1, row, port.toString()));
                Switch nb = sw.linkTable.get(port);
                currentSheet.addCell(new Label(2, row, nb.name));

                if (!sw.isConsistent.get(port)) {
                    currentSheet.addCell(new Label(3, row, "[НЕТОЧНЫЙ РЕЗУЛЬТАТ]"));
                }

                row++;
            }

            row += 2;
            currentSheet.addCell(new Label(0, row, "Выделенные", bold));
            currentSheet.addCell(new Label(1, row, "Порт", bold));
            currentSheet.addCell(new Label(2, row, "MAC", bold));
            currentSheet.addCell(new Label(3, row, "IP", bold));
            currentSheet.addCell(new Label(4, row, "Местоположение", bold));
            currentSheet.addCell(new Label(5, row, "Производитель", bold));
            currentSheet.addCell(new Label(6, row, "Описание", bold));
            portNums = sw.fdb.keySet();
            portIt = portNums.iterator();

            while (portIt.hasNext()) {
                Integer portNum = portIt.next();
                Set<String> nodes = sw.fdb.get(portNum);
                if (nodes.size() > 1)
                    continue;
                
                Iterator<String> sit = nodes.iterator();
                String MAC = sit.next();
                Node node = netScanner.getNode(MAC);
                row++;
                currentSheet.addCell(new Label(1, row, portNum.toString()));
                currentSheet.addCell(new Label(2, row, MAC));
                currentSheet.addCell(new Label(3, row, node.getIP()));
                currentSheet.addCell(new Label(5, row, node.getVendor()));
            }
        }

        wb.write();
        wb.close();
    }
}
